package com.base.key.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.base.common.core.page.TableDataInfo;
import com.base.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.base.key.domain.vo.TKeyUseVo;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.base.common.utils.StringUtils;
import com.base.key.domain.bo.TKeyUseBo;
import com.base.key.domain.TKeyUse;
import com.base.key.mapper.TKeyUseMapper;
import com.base.key.service.ITKeyUseService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 密钥在用表Service业务层处理
 *
 * @author base
 * @date 2023-10-17
 */
@RequiredArgsConstructor
@Service
public class TKeyUseServiceImpl implements ITKeyUseService {

    private final TKeyUseMapper baseMapper;

    /**
     * 查询密钥在用表
     */
    @Override
    public TKeyUseVo queryById(String keyCode){
        return baseMapper.selectVoById(keyCode);
    }

    /**
     * 查询密钥在用表列表
     */
    @Override
    public TableDataInfo<TKeyUseVo> queryPageList(TKeyUseBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<TKeyUse> lqw = buildQueryWrapper(bo);
        Page<TKeyUseVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询密钥在用表列表
     */
    @Override
    public List<TKeyUseVo> queryList(TKeyUseBo bo) {
        LambdaQueryWrapper<TKeyUse> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<TKeyUse> buildQueryWrapper(TKeyUseBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<TKeyUse> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getKeyType()), TKeyUse::getKeyType, bo.getKeyType());
        lqw.eq(bo.getKeyLen() != null, TKeyUse::getKeyLen, bo.getKeyLen());
        lqw.eq(StringUtils.isNotBlank(bo.getOperator()), TKeyUse::getOperator, bo.getOperator());
        lqw.eq(StringUtils.isNotBlank(bo.getKeyValue()), TKeyUse::getKeyValue, bo.getKeyValue());
        lqw.eq(StringUtils.isNotBlank(bo.getCheckStatus()), TKeyUse::getCheckStatus, bo.getCheckStatus());
        lqw.eq(StringUtils.isNotBlank(bo.getStatus()), TKeyUse::getStatus, bo.getStatus());
        lqw.eq(StringUtils.isNotBlank(bo.getUserCode()), TKeyUse::getUserCode, bo.getUserCode());
        lqw.eq(StringUtils.isNotBlank(bo.getUserFlag()), TKeyUse::getUserFlag, bo.getUserFlag());
        lqw.eq(bo.getKeyCreateTime() != null, TKeyUse::getKeyCreateTime, bo.getKeyCreateTime());
        lqw.eq(bo.getKeyUpdateTime() != null, TKeyUse::getKeyUpdateTime, bo.getKeyUpdateTime());
        lqw.eq(bo.getKeyStartTime() != null, TKeyUse::getKeyStartTime, bo.getKeyStartTime());
        lqw.eq(bo.getKeyExpireTime() != null, TKeyUse::getKeyExpireTime, bo.getKeyExpireTime());
        return lqw;
    }

    /**
     * 新增密钥在用表
     */
    @Override
    public Boolean insertByBo(TKeyUseBo bo) {
        TKeyUse add = BeanUtil.toBean(bo, TKeyUse.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setKeyCode(add.getKeyCode());
        }
        return flag;
    }

    /**
     * 修改密钥在用表
     */
    @Override
    public Boolean updateByBo(TKeyUseBo bo) {
        TKeyUse update = BeanUtil.toBean(bo, TKeyUse.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(TKeyUse entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除密钥在用表
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<String> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
